﻿using System;
using System.Windows.Forms;
using MingleReport.HttpRequests;
using MingleReport.Services;

namespace MingleReport
{
    public partial class LoginForm : Form
    {
        private readonly IMingleSession session;

        public LoginForm(IMingleSession session)
        {
            this.session = session;
            InitializeComponent();

            //TODO : delete / sample data, to avoid re-typing
            TestUserSettings user = TestUserSettings.External;
            MingleServerField.Text = user.Mingleserver;
            UserNameField.Text = user.Username;
            PasswordField.Text = user.Password;
            ProjectField.Text = user.Projectname;
        }

        private void SubmitButton_Click(object sender, EventArgs e)
        {
            IHttpRequestFactory httpRequestFactory = new HttpRequestFactory();
            ILoginService loginService = new LoginService(MingleServerUrl, httpRequestFactory);
            loginService.Login(UserName, Password);
            session.LoginCookie = loginService.Cookie;
            session.ServerUrl = MingleServerUrl;
            session.ProjectId = Project;
            Close();
        }

        private string UserName
        {
            get { return UserNameField.Text; }
        }

        private string Password
        {
            get { return PasswordField.Text; }
        }

        private string Project
        {
            get { return ProjectField.Text; }
        }

        private string MingleServerUrl
        {
            get { return MingleServerField.Text; }
        }
    }
}